package menus;

import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionListener;
import java.io.IOException;
import util.GoogleMaps;
import util.Menu;
import util.locationService;

/**
 *
 * @author nialldeasy
 */
public class Map extends Menu {

    double lat = 0, lon = 0;
    int zoom = 7;
    locationService myLoc = new locationService(false);
    boolean started = false;
    boolean refresh = false;
    GoogleMaps map = new GoogleMaps();

    protected void execute(Form f) {
        myLoc = new locationService(false);
        map = new GoogleMaps();
        refresh = true;
        update(f);
    }

    public void recycleBin() {
        myLoc=null;
        map=null;
    }


    //...............................................................................//
    public void updatePosition(Form f) {
        double LAT = myLoc.latitude, LON = myLoc.longitude;

        if (LAT != lat || LON != lon || refresh) {
            try {
                System.out.println("Updating Map: Lat: " + lat + " Lon: " + lon+ " zoom: "+zoom);
                f.setBgImage(map.retrieveStaticImage(200, 320, lat, lon, zoom, "png32"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            refresh = false;
        }
    }

    public void update(Form f) {
        // if(f!=null){
        if (!started) {
            started = true;
            myLoc.start();
        }
        updatePosition(f);
        f.repaint();
        //  }
    }

    public void Key_CENTRE(Form f) {
        lat++;
        lon = 10;
        refresh = true;
        update(f);
    }
    public void Key_UP(Form f){
        lat++;
        refresh = true;
        update(f);
    }
    public void Key_DOWN(Form f){
        lat--;
        refresh = true;
        update(f);
    }
    public void Key_LEFT(Form f){
        lon--;
        refresh = true;
        update(f);
    }
    public void Key_RIGHT(Form f){
        lon++;
        refresh = true;
        update(f);
    }
       public void Key_1(Form f){
           System.out.println("one pressed");
        zoom++;
        refresh = true;
        update(f);
    }

    public void Key_4(Form f) {
        super.Key_4(f);
    }


    public void Key_3(Form f) {
        super.Key_3(f);
    }


    public void Key_0(Form f) {
        super.Key_0(f);
        System.out.println(0);
    }

         public void Key_2(Form f){
        zoom--;
        refresh = true;
        update(f);
    }
}
